package org.ws.edu.task1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author sergey.valuy
 * 
 */
public class MenuDispatcher {

	private static String COMMAND_EXIT = "exit";
	private IConsoleHandler mConsoleHandler;

	public MenuDispatcher(IConsoleHandler handler) {
		mConsoleHandler = handler;
	}

	/**
	 * 
	 */
	public void run() {

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while ((line = br.readLine()) != null) {
				String output = mConsoleHandler.handle(line);
				if (output != null) {
					System.out.println(output);
				}

				if (COMMAND_EXIT.equals(output)) {
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
